package sopt.engSearch;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Locale;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

import comm.parsers.DaumSearchSAXParserHandler;
import comm.parsers.YahooSAXParserHandler;
import comm.util.SoptHashMap;

import statics.SoptVars;

public class YahooSearchProc {
	public static ArrayList getYahooPOISearchResult(String q) throws UnsupportedEncodingException{
		ArrayList lt = null;
		
		
		String reqUrl = "http://kr.open.gugi.yahoo.com/service/poi.php";
		reqUrl += "?appid="+SoptVars.Yahoo_API_KEY+
					"&q="+URLEncoder.encode(q, "UTF-8")+
					"&results=100";
		System.out.println("######YAHOO RSS URL##");
		System.out.println(reqUrl);
		try {
			SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
			
			YahooSAXParserHandler xmlHandler = new YahooSAXParserHandler();
			
			
			parser.parse(reqUrl, xmlHandler);
			
			lt = xmlHandler.getItemList();
			
		} catch (ParserConfigurationException e) {
			return lt;
		} catch (IOException e) {
			return lt;
		} catch (SAXException e) {
			return lt;
		}
		
		System.out.println("YAHOO API가 리턴하는값 ");
		System.out.println(lt.toString());
		
		return lt;
		
		
	}
	
	public static String getTranslatedLocalResult(ArrayList lt,int curpage,String category) throws UnsupportedEncodingException{
		if(category==null || category.length()==0){
			category=SoptVars.Menu_Category_Local;
		}
		String result = "";
		int totalcount = 0;
		int count = 0;
		int size = 0;
		if(lt!=null && lt.size()>0){
			SoptHashMap countHm = (SoptHashMap)lt.get(0);
			if(countHm.get("TOTALCOUNT")!=null)
				totalcount = Integer.parseInt(countHm.get("TOTALCOUNT").toString());
			if(curpage==1){
				result +="<div id='resultTotalCountHeader'>" +
						"<div id='resultHeaderCategory'>" +
						"<p>" +
						"Current Search Area : <b>" +
						category +
						"</b>" +
						" <a href='http://kr.gugi.yahoo.com'>powered by Yahoo! gugi</a></p>" +
						"</div>" +
						"<div id='resultHeaderCount'>";
				if(lt.size()<5)
					count = lt.size();
				
				
				result+=
						"<p><span id='countOfResult'>"+5+"</span> Results" +
						" / Total:" +
						totalcount +
						"</p>" +
						"</div>" +
						"<div class='clear'></div>" +
						"</div>";
				
	       		
			}
			result +="<div id='searchResultTotalContainer'>";
			String transTitle = "";
       		String address = "";
       		String mapx="";
       		String mapy="";
       		
			if(category.equals(SoptVars.Menu_Category_Local)){ 
				result+="<div id='showMapBg'>" +
						"<div id='showMapHeader'><span id='mapTitle'></span><input type='button' value='close' onclick='closeMapLayer()'/></div>" +
						"<div id='MapLayer'></div>" +
						"</div>";
				
			size = Integer.parseInt(((SoptHashMap)lt.get(0)).get("TOTALCOUNT").toString());
			if(size>5)size = curpage*5;
				for(int i=(curpage-1)*5;i<size;i++){
					
		       		SoptHashMap tempHm = new SoptHashMap();
		       		tempHm = (SoptHashMap)lt.get(i);
		       		
		       		transTitle = TranslateProc.getTranslatedString(tempHm.get("NAME").toString(),SoptVars.KOR_TO_ENG);
		       		address = TranslateProc.getTranslatedString(
		       				tempHm.get("CITY").toString() + " " +
		       				tempHm.get("COUNTY").toString() + " " +
		       				tempHm.get("STATE").toString()
		       				,SoptVars.KOR_TO_ENG);
		       		
		       		mapx = tempHm.get("LAT").toString();
		       		mapy = tempHm.get("LON").toString();
		       		String aboutlink = "http://maps.google.co.kr/maps/place?q="+
			       		URLEncoder.encode(tempHm.get("NAME").toString(), "UTF-8")+
			       		"&gl=us&hl=us&cad=src:ppiwlink";
		       		
		       		
		       		result +="<div class='g'>";
		       		result +="<p><a href='"+aboutlink+"'>"+
	       				transTitle +
		       			"</a></p>" +
		       			"<p class='translatedDesc'>Address : "+address+"</p>";
		       		
		       		if(mapx.length()>0 && mapy.length()>0)
		       			result+= "<p><input type='button' aboutlink='"+aboutlink+"' title='"+transTitle.replaceAll("<b>", "").replaceAll("</b>", "")+"' value='viewMap' mapx='"+mapx+"' mapy='"+mapy+"' onclick='javascript:showMap(this);' /></p>";
		       		
		       		result+=
       					"</div>";
		       	}
			}
		}
		
		if(size<1)
			result+="<div style='color:orange;'>Sorry, we're cannot founds any results.</div>";
		
		return result;
	}
}
